home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / ccindex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  12.2 KB  |  427 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: ccindex.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 09/17/1997  
  9. // Date Last Modified: 03/17/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The CCIndex class is a persistent string VBD database used
  32. to store various name and address information in a 3" X 5"
  33. index card file format. All the string members can grow and
  34. shrink as needed.
  35. */
  36. // ----------------------------------------------------------- //   
  37. #include "ccindex.h"
  38. #include "strutil.h"
  39.  
  40. unsigned CCIndex::ObjectLength()
  41. {
  42.   unsigned len = StringFileLength(card_name) + \
  43.     StringFileLength(company_name) + \
  44.     StringFileLength(department_name) + \
  45.     StringFileLength(phone_number) + \
  46.     StringFileLength(fax_number)  + \
  47.     StringFileLength(cell_number)  + \
  48.     StringFileLength(beeper_number)  + \
  49.     StringFileLength(email_address)  + \
  50.     StringFileLength(street_address)  + \
  51.     StringFileLength(internet_urls)  + \
  52.     StringFileLength(comments_block);
  53.  
  54.   return len;
  55. }
  56.  
  57. FAU CCIndex::Write()
  58. {
  59.   ObjectHeader oh;
  60.  
  61.   FAU addr = pod->OpenDatabase()->Alloc(ObjectLength() + sizeof(ObjectHeader));
  62.  
  63.   if(!addr) return 0;
  64.   oh.ClassID = GetClassID();
  65.   oh.ObjectID = addr;
  66.  
  67.   WriteObjHeader(oh);
  68.   WriteString(card_name);
  69.   WriteString(company_name);
  70.   WriteString(department_name);
  71.   WriteString(phone_number);
  72.   WriteString(fax_number);
  73.   WriteString(cell_number);
  74.   WriteString(beeper_number);
  75.   WriteString(email_address);
  76.   WriteString(street_address);
  77.   WriteString(internet_urls);
  78.   WriteString(comments_block);
  79.   objectaddress = addr;  
  80.  
  81.   // Add the entry to the Index file
  82.   if (UsingIndex()) {
  83.     EntryKey key(card_name.c_str(), oh.ObjectID, oh.ClassID);
  84.     AddKey(key);
  85.   }
  86.   return addr;
  87. }
  88.  
  89. void CCIndex::Read(FAU Address)
  90. {
  91.   ObjectHeader oh;
  92.  
  93.   ReadObjHeader(oh, Address);
  94.   if(oh.ClassID != GetClassID()) return; // Incorrect object type
  95.  
  96.   ReadString(card_name);
  97.   ReadString(company_name);
  98.   ReadString(department_name);
  99.   ReadString(phone_number);
  100.   ReadString(fax_number);
  101.   ReadString(cell_number);
  102.   ReadString(beeper_number);
  103.   ReadString(email_address);
  104.   ReadString(street_address);
  105.   ReadString(internet_urls);
  106.   ReadString(comments_block);
  107.   objectaddress = oh.ObjectID;
  108. }
  109.  
  110. FAU CCIndex::Find()
  111. // Find object by seaching the database for its primary data members
  112. {
  113.   CCIndex ccindex;
  114.  
  115.   ccindex.card_name = this->card_name; 
  116.   ccindex.company_name = this->company_name; 
  117.   ccindex.department_name = this->department_name; 
  118.   ccindex.phone_number = this->phone_number;
  119.   ccindex.fax_number = this->fax_number;
  120.   ccindex.cell_number = this->cell_number;
  121.   ccindex.beeper_number = this->beeper_number;
  122.   ccindex.email_address = this->email_address;
  123.   ccindex.street_address = this->street_address;
  124.   ccindex.internet_urls = this->internet_urls;
  125.   ccindex.comments_block = this->comments_block;
  126.  
  127.   int rv; // Return value
  128.  
  129.   // Search the index file for this entry
  130.   if(UsingIndex()) {
  131.     EntryKey e(this->card_name.c_str());
  132.     rv = FindKey(e);
  133.     if(rv) { // Found the object in the index file
  134.       Read(e.object_address);
  135.       objectaddress = e.object_address;
  136.       return e.object_address;
  137.     }
  138.     else
  139.       return 0;
  140.   }
  141.  
  142.   FAU oa;          // Object Address
  143.   VBHeader vb;     // Variable Block Header
  144.   ObjectHeader oh; // Object Header
  145.   
  146.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  147.   FAU addr = 0;
  148.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  149.  
  150.   if(addr == 0) return 0; // No variable blocks found in file
  151.   
  152.   while(1) { 
  153.     if(addr >= vbdfileEOF) break;
  154.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  155.     if(vb.CkWord == CheckWord) {
  156.       if((__SBYTE__)vb.Status == NormalVB) {
  157.     oa = addr + sizeof(VBHeader);
  158.     ReadObjHeader(oh, oa);
  159.     if(oh.ClassID == GetClassID()) { 
  160.       Read(oa);
  161.       if(card_name == ccindex.card_name) {
  162.         objectaddress = oa;
  163.         return oa; // Found unique data member
  164.       }
  165.     }
  166.       }
  167.       addr = addr + vb.Length; // Goto the next variable block
  168.     }
  169.     else {
  170.       addr = pod->OpenDatabase()->VBSearch(addr); 
  171.       if(!addr) break;
  172.     }
  173.   }
  174.  
  175.   // Reset the objects data
  176.   this->card_name = ccindex.card_name;
  177.   this->company_name = ccindex.company_name;
  178.   this->department_name = ccindex.department_name;
  179.   this->phone_number = ccindex.phone_number;
  180.   this->fax_number = ccindex.fax_number;
  181.   this->cell_number = ccindex.cell_number;
  182.   this->beeper_number = ccindex.beeper_number;
  183.   this->email_address = ccindex.email_address;
  184.   this->street_address = ccindex.street_address;
  185.   this->internet_urls = ccindex.internet_urls;
  186.   this->comments_block = ccindex.comments_block;
  187.   
  188.   return 0; // Could not find 
  189. }
  190.  
  191. void CCIndex::Copy(const CCIndex &ob)
  192. {
  193.   card_name = ob.card_name;
  194.   company_name = ob.company_name;
  195.   department_name = ob.department_name;
  196.   phone_number = ob.phone_number;
  197.   fax_number = ob.fax_number;
  198.   cell_number = ob.cell_number;
  199.   beeper_number = ob.beeper_number;
  200.   email_address = ob.email_address;
  201.   street_address = ob.street_address;
  202.   internet_urls = ob.internet_urls;
  203.   comments_block = ob.comments_block;
  204.   objectaddress = ob.objectaddress;
  205. }
  206.  
  207. int CCIndex::FullCompare(const CCIndex &ob)
  208. {
  209.   if(card_name != ob.card_name) return 0;
  210.   if(company_name != ob.company_name) return 0;
  211.   if(department_name != ob.department_name) return 0;
  212.   if(phone_number != ob.phone_number) return 0;
  213.   if(fax_number != ob.fax_number) return 0;
  214.   if(cell_number != ob.cell_number) return 0;
  215.   if(beeper_number != ob.beeper_number) return 0;
  216.   if(email_address != ob.email_address) return 0;
  217.   if(street_address != ob.street_address) return 0;
  218.   if(internet_urls != ob.internet_urls) return 0;
  219.   if(comments_block != ob.comments_block) return 0;
  220.   return 1;
  221. }
  222.  
  223. int operator==(const CCIndex &a, const CCIndex &b)
  224. {
  225.   return CaseICmp(a.GetCardName(), b.GetCardName()) == 0;
  226. }
  227.  
  228. int operator!=(const CCIndex &a, const CCIndex &b)
  229. {
  230.   return CaseICmp(a.GetCardName(), b.GetCardName()) != 0;
  231. }
  232.  
  233. int operator>(const CCIndex &a, const CCIndex &b)
  234. {
  235.   return CaseICmp(a.GetCardName(), b.GetCardName()) > 0;
  236. }
  237.  
  238. int operator>=(const CCIndex &a, const CCIndex &b)
  239. {
  240.   return CaseICmp(a.GetCardName(), b.GetCardName()) >= 0;
  241. }
  242.   
  243. int operator<(const CCIndex &a, const CCIndex &b)
  244. {
  245.   return CaseICmp(a.GetCardName(), b.GetCardName()) < 0;
  246. }
  247.   
  248. int operator<=(const CCIndex &a, const CCIndex &b)
  249. {
  250.   return CaseICmp(a.GetCardName(), b.GetCardName()) <= 0;
  251. }
  252.  
  253. FAU CCIndex::Delete()
  254. {
  255.   if(UsingIndex()) {
  256.     EntryKey e(this->card_name.c_str());
  257.     int rv = FindKey(e);
  258.     if(rv) {  // Found the object in the index file
  259.       FAU addr = e.object_address;
  260.       DeleteObject(e.object_address); // Delete from the data file
  261.       RemoveKey(e);                   // Remove from the index file
  262.       return addr;
  263.     }
  264.     else
  265.       return 0; // Could not delete
  266.   }
  267.  
  268.   FAU addr = Find();
  269.   if(!addr) return 0; // Object does not exist
  270.   DeleteObject(addr);
  271.   return addr;
  272. }
  273.  
  274. FAU CCIndex::Remove()
  275. {
  276.   if(UsingIndex()) {
  277.     EntryKey e(this->card_name.c_str());
  278.     int rv = FindKey(e);
  279.     if(rv) {  // Found the object in the index file
  280.       FAU addr = e.object_address;
  281.       RemoveObject(e.object_address); // Remove from the data file
  282.       RemoveKey(e);                   // Remove from the index file
  283.       return addr;
  284.     }
  285.     else
  286.       return 0; // Could not remove
  287.   }
  288.  
  289.   FAU addr = Find();
  290.   if(!addr) return 0; // Object does not exist
  291.   RemoveObject(addr);
  292.   return addr;
  293. }
  294.  
  295. int CCIndex::CompareIndex()
  296. // Compares the data file to the index file.
  297. // Returns true if data and index file match.
  298. {
  299.   if(!UsingIndex()) return 0;
  300.  
  301.   CCIndex ccindex(pod);
  302.   EntryKey key;
  303.   
  304.   FAU oa;          // Object Address
  305.   VBHeader vb;     // Variable Block Header
  306.   ObjectHeader oh; // Object Header
  307.   
  308.   int objects = 0; // Keeps track of good variable blocks
  309.   int matches = 0; // Keep track of matches
  310.   
  311.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  312.   FAU addr = 0;
  313.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  314.  
  315.   if(addr == 0) { // No variable blocks found in file
  316. #ifdef CPP_EXCEPTIONS
  317.     throw CNoObjectsExist();
  318. #else
  319.     Error->SignalException(EHandler::NoObjectsExist, EHandler::DISPLAY);
  320.     return 0;
  321. #endif
  322.   }
  323.   
  324.   while(1) { 
  325.     if(addr >= vbdfileEOF) break;
  326.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  327.     if(vb.CkWord == CheckWord) {
  328.       if((__SBYTE__)vb.Status == NormalVB) {
  329.     oa = addr + sizeof(VBHeader);
  330.     ReadObjHeader(oh, oa);
  331.     if(oh.ClassID == GetClassID()) { 
  332.       objects++; // Increment the object count
  333.       ccindex.Read(oa);
  334.       key.SetStrKey(ccindex.GetCardName());
  335.       key.SetOA(oa);
  336.       key.SetCID(oh.ClassID);
  337.       int rv = FindKey(key, 1);
  338.       if(rv) matches++; // Index and data file match
  339.     }
  340.       }
  341.       addr = addr + vb.Length; // Goto the next variable block
  342.     }
  343.     else {
  344.       addr = pod->OpenDatabase()->VBSearch(addr); 
  345.       if(!addr) break;
  346.     }
  347.   }
  348.  
  349.   return objects == matches;
  350. }
  351.  
  352. int CCIndex::RebuildIndexFile(const char *fname)
  353. {
  354.   if(!UsingIndex()) return 0;
  355.  
  356.   int rv; // (R)eturn (V)alue
  357.   int CacheSize = 15;
  358.  
  359.   VBDFilePtr f(new VBDFile);
  360.   Btree btx(CacheSize);
  361.   f->Create(fname, sizeof(BtreeHeader));
  362.   btx.Connect(f, 1);
  363.   
  364.   CCIndex ccindex(pod);
  365.   EntryKey key;
  366.   
  367.   FAU oa;          // Object Address
  368.   VBHeader vb;     // Variable Block Header
  369.   ObjectHeader oh; // Object Header
  370.   
  371.   int objects = 0; // Keeps track of good variable blocks
  372.   int inserts = 0; // Keep track of inserts
  373.   
  374.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  375.   FAU addr = 0;
  376.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  377.  
  378.   if(addr == 0) { // No variable blocks found in file
  379. #ifdef CPP_EXCEPTIONS
  380.     throw CNoObjectsExist();
  381. #else
  382.     Error->SignalException(EHandler::NoObjectsExist, EHandler::DISPLAY);
  383.     return 0;
  384. #endif
  385.   }
  386.   
  387.   while(1) { 
  388.     if(addr >= vbdfileEOF) break;
  389.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  390.     if(vb.CkWord == CheckWord) {
  391.       if((__SBYTE__)vb.Status == NormalVB) {
  392.     oa = addr + sizeof(VBHeader);
  393.     ReadObjHeader(oh, oa);
  394.     if(oh.ClassID == GetClassID()) { 
  395.       objects++; // Increment the object count
  396.       ccindex.Read(oa);
  397.       key.SetStrKey(ccindex.GetCardName());
  398.       key.SetOA(oa);
  399.       key.SetCID(oh.ClassID);
  400.       rv = btx.Add(key);
  401.       if(!rv) { 
  402. #ifdef CPP_EXCEPTIONS
  403.         throw CAssertError();
  404. #else
  405.       Error->SignalException(EHandler::AssertError);
  406. #endif
  407.       }
  408.       else
  409.         inserts++; // Index and data file match
  410.     }
  411.       }
  412.       addr = addr + vb.Length; // Goto the next variable block
  413.     }
  414.     else {
  415.       addr = pod->OpenDatabase()->VBSearch(addr); 
  416.       if(!addr) break;
  417.     }
  418.   }
  419.  
  420.   return objects == inserts;
  421. }
  422. // ----------------------------------------------------------- // 
  423. // ------------------------------- //
  424. // --------- End of File --------- //
  425. // ------------------------------- //
  426.  
  427.